package com.just.demo.controller;


import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.Optional;


@Component
public class CityHandler {
    /**
     * ServerResponse 是对响应的封装，可以设置响应状态，响应头，响应正文。
     * 比如 ok 代表的是 200 响应码、
     * MediaType 枚举是代表这文本内容类型、
     * 返回的是 String 的对象。
     * @param request
     * @return
     */

    public Mono<ServerResponse> helloCity(ServerRequest request ){
        return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("hello city!!"));

    }


    public Mono<ServerResponse> helloCity1(ServerRequest request) {
        return ServerResponse.ok().body(sayHelloCity(request), String.class);
    }

    private Mono<String> sayHelloCity(ServerRequest request) {
        Optional<String> cityParamOptional = request.queryParam("city");
        if (!cityParamOptional.isPresent()) {
            //throw new GlobalException(HttpStatus.INTERNAL_SERVER_ERROR, "request param city is ERROR");
        }

        return Mono.just("Hello," + cityParamOptional.get());
    }

}
